home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_03 / pjp / tsstream.c < prev   
Encoding:
C/C++ Source or Header  |  1995-02-02  |  2.7 KB  |  81 lines

  1. ------------- Listing 7: The file tsstream.c ------------------
  2.  
  3. // test <sstream>
  4. #include <cassert>
  5. #include <iostream>
  6. #include <sstream>
  7.  
  8. void t1()
  9.         {       // test stringbuf
  10.         _STD string s0("s0"), s1("s1"), s2("s2"), s3("s3");
  11.         _STD stringbuf sb0, sb1(_STD ios::in), sb2(_STD ios::out),
  12.                 sb3(_STD ios::in | _STD ios::out);
  13.         _STD stringbuf sb10(s0), sb11(s1, _STD ios::in),
  14.                 sb12(s2, _STD ios::out),
  15.                 sb13(s3, _STD ios::in | _STD ios::out);
  16.         _STD ostream outs(&sb0);
  17.         outs << "dynamic stringbuf 0";
  18.         s3 = sb0.str();
  19.         assert(s3 == "dynamic stringbuf 0");
  20.         sb0.str(s0);
  21.         assert(sb0.str() == "s0");
  22.         outs.rdbuf(&sb2);
  23.         outs << "dynamic stringbuf 2";
  24.         assert(sb2.str() == "dynamic stringbuf 2");
  25.         outs.rdbuf(&sb10);
  26.         outs << "x";
  27.         assert(sb10.str() == "x0");
  28.         outs.rdbuf(&sb11);
  29.         outs << "x";
  30.         assert(!outs.good() && sb11.str() == "s1");
  31.         outs.rdbuf(&sb12);
  32.         outs << "x";
  33.         assert(sb12.str() == "x");
  34.         assert(sb12.pubseekoff(2, _STD ios::beg).offset() == 2
  35.                 && sb12.str() == "x2");
  36.         }
  37.  
  38. void t2()
  39.         {       // test istringstream
  40.         _STD string s0("s0"), s1("s1"), s2("s2"), s3("s3");
  41.         _STD istringstream is0, is1(_STD ios::in),
  42.                 is2(_STD ios::out), is3(_STD ios::in | _STD ios::out);
  43.         _STD istringstream is10(s0), is11(s1, _STD ios::in),
  44.                 is12(s2, _STD ios::out),
  45.                 is13(s3, _STD ios::in | _STD ios::out);
  46.         assert(is10.rdbuf()->str() == "s0");
  47.         assert(is11.str() == "s1");
  48.         is0.str("abc");
  49.         assert(is0.str() == "abc");
  50.         is0 >> s0;
  51.         assert(s0 == "abc");
  52.         }
  53.  
  54. void t3()
  55.         {       // test ostringstream
  56.         _STD string s0("s0"), s1("s1"), s2("s2"), s3("s3");
  57.         _STD ostringstream os0, os1(_STD ios::in),
  58.                 os2(_STD ios::out), os3(_STD ios::in | _STD ios::out);
  59.         _STD ostringstream os10(s0), os11(s1, _STD ios::in),
  60.                 os12(s2, _STD ios::out),
  61.                 os13(s3, _STD ios::in | _STD ios::out);
  62.         assert(os10.rdbuf()->str() == "");
  63.         assert(os13.str() == "s3");
  64.         os0.str("abc");
  65.         assert(os0.str() == "");
  66.         assert(os0.rdbuf()->pubseekoff(2, _STD ios::beg).offset()
  67.                 == 2 && os0.str() == "ab");
  68.         os0 << "Cde";
  69.         assert(os0.str() == "abCde");
  70.         }
  71.  
  72. int main()
  73.         {       // test basic workings of stringstream definitions
  74.         t1();
  75.         t2();
  76.         t3();
  77.         _STD cout << "SUCCESS testing <sstream>" << _STD endl;
  78.         return (0);
  79.         }
  80.  
  81.